package com.lyl.async.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class OrderService {
    //不需要返回值
    @Async("localTaskExecutor")
    public void getAddress(){
        log.info(Thread.currentThread().getName() + "开始执行异步任务 : getAddress");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info(Thread.currentThread().getName() + "异步任务执行完成 : getAddress ");
    }

    //带返回值
    @Async("localTaskExecutor")
    public Future<String> getAge(){
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult<>("芳龄十八");
    }

    //带返回值
    public String getName(){
        log.info(Thread.currentThread().getName() + "开始执行异步任务 : getName");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info(Thread.currentThread().getName() + "异步任务执行完成 : getName ");
        return "getName: 张三";
    }


}
